mui.init();
var subpages = ['pages/home/home.html', 'pages/invest/invest.html', 'pages/account/userAccount.html', 'pages/about/abount.html'];
var subpage_style = {
	top: '0px',
	bottom: '50px'
};
var subpages2 = ['pages/login/login.html'];

function openLocker() {
	//检查 "登录状态/锁屏状态" 开始
	var settings = app.getSettings();
	var userName = plus.storage.getItem(KeyCfg.userName);
	var pwd = plus.storage.getItem(KeyCfg.userPwd);
	var sessionId = plus.storage.getItem(KeyCfg.JSESSIONID);
	if (settings.autoLogin && settings.gestures) {
		var flag = false;
		var date = new Date();
		var times = date.getTime();
		if (sessionId && ((times - settings.overTime) > 1000 * 60*2 && userName && pwd && userName!=null && pwd!=null)) {
			flag = true;
		}
		if (flag) {
			var all = plus.webview.all();
			var locker;
			for(var i=0,j=all.length;i<j;i++){
				if(all[i].id=='unlock'){
					locker = all[i];
					break;
				}
			}
			if(locker){
				locker.show();
			}else{
				mui.openWindow({
					url: 'pages/openlocker/openlocker.html',
					id: 'unlock',
					show: {
						aniShow: 'pop-in'
					},
					waiting: {
						autoShow: false
					}
				});
			}
		}
	}
}
document.addEventListener('resume', function() {
	openLocker();
});
//创建子页面，首个选项卡页面显示，其它均隐藏；
mui.plusReady(function() {
	document.addEventListener("netchange",function(){
		var nt = plus.networkinfo.getCurrentType();
		switch(nt) {
			case plus.networkinfo.CONNECTION_ETHERNET:
			case plus.networkinfo.CONNECTION_WIFI:
			plus.nativeUI.toast('切换到WIFI网络！');
			break; 
			case plus.networkinfo.CONNECTION_CELL2G:
			case plus.networkinfo.CONNECTION_CELL3G:
			case plus.networkinfo.CONNECTION_CELL4G:
			plus.nativeUI.toast('切换到移动网络！');
			break;
			default:
			plus.nativeUI.toast('网络断开，请检查网络！');
			break;
		}
	},false);
	plus.key.addEventListener('backbutton',function(e){});
	openLocker();
	var self = plus.webview.currentWebview();
	var subPages = [];
	for (var i = 0; i < 4; i++) {
		var sub = plus.webview.create(subpages[i], subpages[i], subpage_style);
		if (i > 0) {
			sub.hide();
		}
		self.append(sub);
		subPages[i] = sub;
	}
	for (var i =0,j=subpages2.length;i<j;i++){
		var sub = plus.webview.create(subpages2[i], subpages2[i], subpage_style);
		sub.hide();
		self.append(sub);
	}
	addLoadedListeners(subPages);
});

var pageReadyCfg = {};

function addLoadedListeners(subPages) {
	var homePage = subPages[0];
	var homeId = homePage.id;
	homePage.addEventListener('loaded', function(e) {
		pageReadyCfg[homeId] = true;
	});

	var investPage = subPages[1];
	var investId = investPage.id;
	investPage.addEventListener('loaded', function(e) {
		pageReadyCfg[investId] = true;
	});

	var accountPage = subPages[2];
	var accountId = accountPage.id;
	accountPage.addEventListener('loaded', function(e) {
		pageReadyCfg[accountId] = true;
	});

	var aboutPage = subPages[3];
	var aboutId = aboutPage.id;
	aboutPage.addEventListener('loaded', function(e) {
		pageReadyCfg[aboutId] = true;
	});
}

//当前激活选项
var activeTab = subpages[0];
var title = document.getElementById("title");

function openTab(_this, targetTab) {
	//更换标题
	//title.innerHTML = _this.querySelector('.mui-tab-label').innerHTML;
	//显示目标选项卡
	if(_this==null){
		plus.webview.getWebviewById(targetTab).show();
		activeTab = subpages[2];
	}else{
		plus.webview.getWebviewById(targetTab).show();
		//隐藏当前;
		plus.webview.getWebviewById(activeTab).hide();
		//更改当前活跃的选项卡
		activeTab = targetTab;
	}
}
//选项卡点击事件
mui('.mui-bar-tab').on('tap', 'a', function(e) {
	if(e.detail.tabId == '1'){
		plus.storage.setItem('moneychange','1');
	}else{
		plus.storage.removeItem('moneychange');
	}
	var tabId = this.id;
	var _this = this;
	var targetTab = this.getAttribute('href');
	if (targetTab == activeTab) {
		return;
	}
	var pageReady = pageReadyCfg[targetTab];
	if (!pageReady) { /*如果子页面还没加载完成，延时半秒打开页面*/
		window.setTimeout(function() {
			startOpen(_this, tabId, targetTab);
		}, 500);
	} else {
		startOpen(_this, tabId, targetTab);
	}
});

function startOpen(_this, tabId, targetTab) {
	if (tabId == "userAccount") {
		var targetPageId = plus.webview.currentWebview().id;
		plus.storage.setItem(KeyCfg.targetPageId, targetPageId);
		plus.storage.setItem(KeyCfg.currClickTabId, tabId);
		plus.storage.removeItem(KeyCfg.prevUrl);
		MuiEventMgr.basePath = 'pages/';
		MuiEventMgr.checkSession(function() {
			openTab(_this, targetTab);
			refreshSubPages(tabId);
		}, function() {
			openTab(null,subpages2[0]);
			refreshSubPages(subpages2[0]);
		});
	} else {
		openTab(_this, targetTab);
		refreshSubPages(tabId);
	}
}



//自定义事件，模拟点击“首页选项卡”
document.addEventListener('gohome', function() {
	activeTabItemPage("defaultTab");
});
function activeTabItemPage(tabItemId,data) {
	var defaultTab = document.getElementById(tabItemId);
	//模拟首页点击
	mui.trigger(defaultTab, 'tap',{tabId:data});
	//切换选项卡高亮
	var current = document.querySelector(".mui-bar-tab>.mui-tab-item.mui-active");
	if (defaultTab !== current) {
		current.classList.remove('mui-active');
		defaultTab.classList.add('mui-active');
	}
}

/**
 * 当从登录页面返回时，回调用当前“刷新”事件
 */
window.addEventListener('refresh', function(event) {
	var currClickTabId = plus.storage.getItem(KeyCfg.currClickTabId);
	console.log("currClickTabId=" + currClickTabId);
	if (!currClickTabId) return;
	var ele = document.getElementById(currClickTabId);
	var targetTab = ele.getAttribute('href');
	activeTabItemPage(currClickTabId);
	refreshSubPages(currClickTabId);
	//移除当前点击过的Tab选项卡,以避免当登录成功后自动切换到我的帐户页面
	plus.storage.removeItem(KeyCfg.currClickTabId);
	plus.storage.removeItem(KeyCfg.targetPageId);
});

window.addEventListener("activePage", function(event) {
	var userAccountPageId = 'pages/account/userAccount.html';
	var targetPage = plus.webview.getWebviewById(userAccountPageId);
	console.log("userAccount.isVisible=" + targetPage.isVisible());
	mui.alert(targetPage.isVisible());
	refreshSubPages("userAccount");
});

function refreshSubPages(currClickTabId) {
	var currClickTab = document.getElementById(currClickTabId);
	if(!currClickTab){
		return;
	}
	var href = currClickTab.getAttribute('href');
	if (currClickTabId == "userAccount") {
		var userAccountPageId = 'pages/account/userAccount.html';
		var targetPage = plus.webview.getWebviewById(userAccountPageId);
		mui.fire(targetPage, 'reload', {});
	} else if (currClickTabId == "defaultTab") { /*首页*/
		var homePageId = 'pages/home/home.html';
		var targetPage = plus.webview.getWebviewById(homePageId);
		mui.fire(targetPage, 'reload', {});
	} else {
		var targetPage = plus.webview.getWebviewById(href);
		mui.fire(targetPage, 'reload', {});
	}
}